home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / JOY.C < prev    next >
Encoding:
Text File  |  1995-02-11  |  2.9 KB  |  127 lines

  1. /*
  2.  
  3.     joy.c
  4.  
  5.     Internet: alexad3@icebox.iceonline.com
  6.  
  7.  
  8.    a C fragment containing (ick!) in-line assembly code
  9.    to read joystick 1, and its buttons
  10.  
  11.  
  12.    You will also need code to calibrate the joystick, but that's
  13.    trivial.
  14.  
  15. */
  16.  
  17.  
  18. /* note!!!! buttons are pressed when b0, b1 is zero (0)
  19.  
  20.    Also, the values returned by this routine will vary widely
  21.    on machines that run at different speeds for joy_x and y
  22.  
  23.    reads joy1 only
  24.  
  25.    if joystick is not plugged in the joy_x and joy_y will both
  26.    be 1023
  27.  
  28.    generaly you read the joystick at fixed intervals because
  29.    it is a slow process, and the buttons more frequently.
  30.  
  31. */
  32.  
  33. short joy_x, joy_y;
  34. BYTE b0, b1;
  35.  
  36.  
  37. /* sets the global vars b1, and b0 */
  38. /* ---------------------- read_joy_buttons() -------------- June 10,1993 */
  39. void read_joy_buttons(void)
  40. {
  41.    asm {
  42.  
  43.                mov    dx,201h
  44.                xor    ax, ax
  45.                in     al,dx
  46.  
  47.                mov    bl, al
  48.                and    bl, 010h
  49.                mov    b0, bl
  50.  
  51.                and    al, 020h
  52.                mov    b1, al
  53.        }
  54. }
  55.  
  56.  
  57. /* sets the global vars joy_x, joy_y, b0, b1 - SLOW!!! */
  58. /* this is slow compared to just checking the buttons.
  59.    in many projects I only check the joystick once/(certain time)
  60.    and the buttons more frequently.
  61. */
  62.    
  63. /* ---------------------- read_joy() --------------------- March 29,1993 */
  64. void read_joy(void)
  65. {
  66.    asm {
  67.                push   si
  68.  
  69.                xor    ax,ax
  70. /* joystick port */
  71.                mov    dx,201h
  72.                mov    cx,ax
  73.                mov    bx,ax
  74.                mov    si, 1024   /* this number may have to be LARGER on
  75.                                   FAST machines */
  76.  
  77.                cli    /* disable interupts so that our timing loop isn't
  78.                          interupted */
  79.  
  80. /* begin digitize, prime joy port */
  81.                out    dx,al
  82.        }
  83. jlp:           
  84.    asm {
  85.                dec    si
  86.                jz     abt          /* timeout */
  87. /* read port */
  88.                in     al,dx
  89.                test   al,1
  90.                jz     nox
  91. /* increment x counter if bit 0 high */
  92.                inc    bx
  93.        }
  94. nox:           
  95.    asm {
  96.                test   al,2
  97.                jz     noy
  98. /* increment y counter if bit 1 high */
  99.                inc    cx
  100.        }
  101. noy:           
  102.    asm {
  103.                test   al,3
  104. /* keep going until both x and y done */
  105.                jnz    jlp
  106.        }
  107. abt:           
  108.    asm {
  109.                sti    /* enable interupts */
  110.  
  111. /* store x and y coordinates, and button stats */
  112.  
  113.                mov    joy_y, cx
  114.                mov    joy_x, bx
  115.  
  116.                mov    bl, al
  117.                and    bl, 010h
  118.                mov    b0, bl
  119.  
  120.                and    al, 020h
  121.                mov    b1, al
  122.  
  123.                pop    si
  124.        }
  125.  
  126. }
  127.